package com.jarvis.hospital;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jarvis.hospital.adapter.OperationAdapter;
import com.jarvis.hospital.common.BaseActivity;
import com.jarvis.hospital.common.ResponseData;
import com.jarvis.hospital.model.LoginModel;
import com.jarvis.hospital.model.OpertionDetailModel;
import com.jarvis.hospital.model.OpertionDetailReponseModel;
import com.jarvis.hospital.model.OpertionListModel;
import com.jarvis.hospital.model.OpertionModel;
import com.jarvis.hospital.model.UserInfoModel;
import com.jarvis.hospital.servers.WebService;
import com.jarvis.hospital.util.SharePreferenceKey;
import com.jarvis.hospital.util.SharePreferenceUtils;
import com.jarvis.hospital.util.net.RequestApi;

import java.util.ArrayList;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class OperationDetailActivity extends BaseActivity {
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.tv_bed_number)
    TextView tv_bed_number;
    @BindView(R.id.tv_patient_name)
    TextView tv_patient_name;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_medicine_name)
    TextView tv_medicine_name;
    @BindView(R.id.tv_time_duration)
    TextView tv_time_duration;
    @BindView(R.id.tv_dose)
    TextView tv_dose;
    @BindView(R.id.tv_taskowner)
    TextView tv_taskowner;
    @BindView(R.id.tv_deal)
    TextView tv_deal;
    @BindView(R.id.lin_medicine)
    LinearLayout lin_medicine;

    private WebService webService;
    private String taskId;
    private String status;  //0:处理事件 1：抢单事件

    @Override
    public int getLayoutId() {
        return R.layout.activity_orperation_detail;
    }

    @Override
    public void initData() {
        Bundle bundle = this.getIntent().getExtras();
        taskId = bundle.getString("taskId");
        status = bundle.getString("status");
        if("0".equals(status)){
            lin_medicine.setVisibility(View.VISIBLE);
            tv_taskowner.setVisibility(View.GONE);
            tv_deal.setVisibility(View.VISIBLE);
        }else {
            tv_taskowner.setVisibility(View.VISIBLE);
            tv_deal.setVisibility(View.GONE);
            lin_medicine.setVisibility(View.GONE);
        }
        loadData();
    }
    private void showReviewDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View customView = inflater.inflate(R.layout.dia_notice, null);
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(customView);
        dialog.setTitle("温馨提示");
        dialog.show();
        TextView tv_back = (TextView) customView.findViewById(R.id.tv_back);
        TextView tv_pass = (TextView) customView.findViewById(R.id.tv_pass);
        tv_pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                dealTask();
            }
        });
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    @Override
    public void initView() {
        webService = RequestApi.create(WebService.class);
        tv_deal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showReviewDialog();
            }
        });
        tv_taskowner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                taskowner();
            }
        });

    }
    private void showDate(OpertionDetailModel opertionDetailModel){
        tv_content.setText(opertionDetailModel.getTaskContent());
        tv_bed_number.setText(opertionDetailModel.getTaskPatientBedNumber());
        tv_patient_name.setText(opertionDetailModel.getTaskPatientName());
        tv_time.setText(opertionDetailModel.getTaskTimePlan());
        tv_medicine_name.setText(opertionDetailModel.getTaskMedicineName());
        tv_time_duration.setText(opertionDetailModel.getTimeDuration());
        tv_dose.setText(opertionDetailModel.getDose());
    }
    private void dealTask(){
        showDialog("加载中");
        webService.updateTaskTimeAct(taskId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData opertionDetailModelResponseData) {
                        if (opertionDetailModelResponseData != null) {
                            int resultCode = opertionDetailModelResponseData.getResultCode();
                            if (resultCode == 200) {
                                finish();
                            } else {
                                showTextToast(opertionDetailModelResponseData.getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });
    }
    private void taskowner(){
        UserInfoModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
        String userId = userModel.getUserId();
        showDialog("加载中");
        webService.getTaskOwnershipOnWatch(taskId,userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData opertionDetailModelResponseData) {
                        if (opertionDetailModelResponseData != null) {
                            int resultCode = opertionDetailModelResponseData.getResultCode();
                            finish();
                            showTextToast(opertionDetailModelResponseData.getMessage());
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });
    }
    private void loadData() {
        showDialog("加载中");
        webService.getTaskInfoOnWatch(taskId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData<OpertionDetailReponseModel>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData<OpertionDetailReponseModel> opertionDetailModelResponseData) {
                        if (opertionDetailModelResponseData != null) {
                            int resultCode = opertionDetailModelResponseData.getResultCode();
                            if (resultCode == 200) {
                                showDate(opertionDetailModelResponseData.getData().getTaskInfo());
                            } else {
                                showTextToast(opertionDetailModelResponseData.getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });
    }
}
